﻿

namespace Acoris.Content
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics;
    using System.Text;

    /// <summary>
    /// 表示内容页
    /// </summary>
    [DebuggerDisplay("count:{Count}")]
    public sealed class Page : NamedObject, ICollection<Section>, IEnumerable<Section>
    {
        internal ObservableCollection<Section> m_sectionCollection;
        internal object m_lock;

        internal Page()
        {
            m_sectionCollection = new ObservableCollection<Section>();
            m_lock = new object();
        }

        #region ICollection<Section>接口成员

        /// <summary>
        /// 获取当前集合的元素个数
        /// </summary>
        public int Count {
            get { return m_sectionCollection.Count; }
        }

        /// <summary>
        /// 获取一个值,该值指示当前集合的只读性
        /// </summary>
        public bool IsReadOnly {
            get { return false; }
        }

        /// <summary>
        /// 将指定内容页添加到集合之中
        /// </summary>
        /// <param name="item">要添加的对象</param>
        public void Add(Section item)
        {
            lock (m_lock) {
                m_sectionCollection.Add(item);
            }
        }

        /// <summary>
        /// 清空当前集合
        /// </summary>
        public void Clear()
        {
            lock (m_lock) {
                m_sectionCollection.Clear();
            }
        }

        /// <summary>
        /// 获取一个值，该值指示指定的元素是否存在与当前集合
        /// </summary>
        /// <param name="item">要比较的对象</param>
        /// <returns>如果指定的对象存在于当前集合之中则返回true，否则返回false</returns>
        public bool Contains(Section item)
        {
            var result = false;
            lock (m_lock) {
                result = m_sectionCollection.Contains(item);
            }
            return result;
        }

        /// <summary>
        /// 将当前集合的元素复制到指定的数组
        /// </summary>
        /// <param name="array">目标数组</param>
        /// <param name="arrayIndex">复制操作在目标数组的起始位置</param>
        public void CopyTo(Section[] array, int arrayIndex)
        {
            m_sectionCollection.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// 删除集合中的指定元素
        /// </summary>
        /// <param name="item">要删除的对象</param>
        /// <returns>如果删除成功则返回true，否则返回false</returns>
        public bool Remove(Section item)
        {
            var result = false;
            lock (m_lock) {
                result = m_sectionCollection.Remove(item);
            }
            return result;
        }

        #endregion

        #region IEnumerable<Section>接口成员

        /// <summary>
        /// 获取用于当前对象的枚举数
        /// </summary>
        /// <returns>返回用于当前对象的枚举数</returns>
        public IEnumerator<Section> GetEnumerator()
        {
            return ((IEnumerable<Section>)this.m_sectionCollection).GetEnumerator();
        }

        /// <summary>
        /// 获取用于当前对象的枚举数
        /// </summary>
        /// <returns>返回用于当前对象的枚举数</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<Section>)this.m_sectionCollection).GetEnumerator();
        }

        #endregion
    }
}
